//
//  averageWidthCollectionViewController.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 8/10/15.
//  Copyright (c) 2015 dvlproad. All rights reserved.
//

#import "TSCellSelectCollectionViewController.h"
#import <CQDemoKit/CQTSContainerViewFactory.h>
#import <CQDemoKit/CQTSButtonFactory.h>
#import <CQDemoKit/CQTSNetImagesUtil.h>
#import <UICollectionViewCJHelper/UICollectionView+CJSelect.h>

#import "TSCellSelectCollectionView.h"

@interface TSCellSelectCollectionViewController () {
    
}
@property (nonatomic, strong) TSCellSelectCollectionView *collectionView;
@property (nonatomic, assign, readonly) BOOL isChoosing;
@end

@implementation TSCellSelectCollectionViewController

- (BOOL)automaticallyAdjustsScrollViewInsets {
    return NO;
}

- (void)__chooseBarAction:(UIBarButtonItem *)chooseBarButtonItem {
    _isChoosing = !self.isChoosing;
    
    if (_isChoosing) {
        chooseBarButtonItem.title = @"取消";
        self.collectionView.isChoosing = YES;
    } else {
        chooseBarButtonItem.title = @"选择";
        self.collectionView.isChoosing = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"Cell的Select";
    
    UIBarButtonItem *chooseBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"选择" style:UIBarButtonItemStylePlain target:self action:@selector(__chooseBarAction:)];
    _isChoosing = NO;
    self.navigationItem.rightBarButtonItems = @[chooseBarButtonItem];
    

    TSCellSelectCollectionView *collectionView = [[TSCellSelectCollectionView alloc] init];
    collectionView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).mas_offset(120);
        make.left.mas_equalTo(self.view).mas_offset(10);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(260);
    }];
    self.collectionView = collectionView;
    collectionView.allowsMultipleSelection = YES; //是否打开多选
    
//    self.collectionView.dataCellActionType = DataCellActionTypeSelect;
    self.collectionView.alwaysAloneIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];//用于测试"我与其他不共存"
    
    // 反选 VS 全选
    UIView *selectButtonsView = [CQTSContainerViewFactory twoButtonsViewAlongAxis:MASAxisTypeHorizontal title1:@"反选" actionBlock1:^(UIButton * _Nonnull bButton) {
        [self invertselect:bButton];
    } title2:@"全选" actionBlock2:^(UIButton * _Nonnull bButton) {
        [self selectAll:bButton];
    }];
    [self.view addSubview:selectButtonsView];
    [selectButtonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(collectionView.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(self.view).mas_offset(10);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
        
    // reload 保存已选中 VS 放弃已选中 (测试cell的selected状态)
    UIView *reloadButtonsView = [CQTSContainerViewFactory twoButtonsViewAlongAxis:MASAxisTypeVertical title1:@"reloadData并保存已选中cell的选中状态" actionBlock1:^(UIButton * _Nonnull bButton) {
        [self reloadCollectionViewWithKeepSelected];
    } title2:@"reloadData并放弃已选中cell的选中状态" actionBlock2:^(UIButton * _Nonnull bButton) {
        [self reloadCollectionViewWithGiveupSelected];
    }];
    [self.view addSubview:reloadButtonsView];
    [reloadButtonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(selectButtonsView.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(self.view).mas_offset(10);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(2*44+10);
    }];
    
    // printSelectedItems
    UIButton *printSelectedItemsButton = [CQTSButtonFactory themeBGButtonWithTitle:@"打印当前的selectedItemsButton" actionBlock:^(UIButton * _Nonnull bButton) {
        NSArray *indexPathsForSelectedItems = [self.collectionView indexPathsForSelectedItems];
        NSLog(@"indexPathsForSelectedItems = %@", indexPathsForSelectedItems);
        //总结：即使选中的item，被移动到屏幕外，indexPathsForSelectedItems也是不会变的；但是reloadData的话，就会导致所有的indexPathsForSelectedItems为空
    }];
    [self.view addSubview:printSelectedItemsButton];
    [printSelectedItemsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(reloadButtonsView.mas_bottom).mas_offset(30);
        make.left.mas_equalTo(reloadButtonsView);
        make.centerX.mas_equalTo(reloadButtonsView);
        make.height.mas_equalTo(44);
    }];
        
    
    self.collectionView.dataModels = [CQTSNetImagesUtil __getTestNetImageDataModels];
    [self.collectionView reloadData];
}


#pragma mark - ReloadEvent
///打印当前的indexPathsForSelectedItems
- (void)printIndexPathsForSelectedItems:(id)sender {
    NSArray *indexPathsForSelectedItems = [self.collectionView indexPathsForSelectedItems];
    NSLog(@"indexPathsForSelectedItems = %@", indexPathsForSelectedItems);
    //总结：即使选中的item,被移动到屏幕外，indexPathsForSelectedItems也是不会变的；但是reloadData的话，就会导致所有的indexPathsForSelectedItems为空
}

///reload (测试cell的selected状态)
- (void)reloadCollectionViewWithKeepSelected {
    [self.collectionView my_reloadDataWithKeepSelectedState:YES];
}

- (void)reloadCollectionViewWithGiveupSelected {
    [self.collectionView my_reloadDataWithKeepSelectedState:NO];
}

#pragma mark - SelectEvent
///测试"全选"功能
- (void)selectAll:(UIButton *)button {
    button.selected = !button.isSelected;
    if (button.selected) { //进行全选
        [self.collectionView my_selectAllAndReloadData];
    } else {
        [self.collectionView reloadData];
    }
}

///测试"反选"功能
- (void)invertselect:(UIButton *)button {
    NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
    [self.collectionView my_invertselectIndexPaths:indexPaths];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
